package com.songshan.controller;

import com.songshan.bean.Role;
import com.songshan.dto.PageParameter;
import com.songshan.dto.Result;
import com.songshan.exception.BusinessException;
import com.songshan.service.RoleManageService;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author feb
 * @date 2024/6/18
 * @Description: 角色管理
 */
@Controller
@RequestMapping(value = "/role")
public class RoleManageController {

  @Autowired
  private RoleManageService roleManageService;

  /**
   * @author feb
   * @date 2024/6/18
   * @Description: 角色管理页面
   */
  @RequestMapping("/manage.htmlx")
  public String roleManage() {
    return "roleManage";
  }

  /**
   * @author feb
   * @date 2024/6/18
   * @Description: 角色管理列表
   */
  @PostMapping("queryWithPage.do")
  @ResponseBody
  public Map<String, Object> queryRoleWithPage(@RequestBody PageParameter pageParameter) {
    return roleManageService.queryRoleWithPage(pageParameter);
  }

  /**
   * @author feb
   * @date 2024/6/18
   * @Description: 角色保存
   */
  @PostMapping("save.do")
  @ResponseBody
  public Result save(@Valid Role role, BindingResult errors) throws BusinessException {
    if (errors.hasErrors()) {
      return Result.failure(errors.getFieldErrors());
    }
    if (role.getId() == null) {
      roleManageService.addRole(role);
    } else {
      roleManageService.modRole(role);
    }
    return Result.success();
  }

  /**
   * @author feb
   * @date 2024/6/18
   * @Description: 删除角色
   */
  @PostMapping("del.do")
  @ResponseBody
  public Result del(Integer id) throws BusinessException{
    roleManageService.delRole(id);
    return Result.success();
  }

  /**
   * @author feb
   * @date 2024/6/19
   * @Description: 角色状态操作
   */
  @PostMapping("roleStatus.do")
  @ResponseBody
  public Result roleStatus(Integer id,Integer status) throws BusinessException{
    roleManageService.roleStatus(id,status);
    return Result.success();
  }

  /**
   * @author feb
   * @date 2024/6/18
   * @Description: 角色关联菜单
   */
  @PostMapping("relateMenu.do")
  @ResponseBody
  public Result relateMenu(@RequestBody Map<String, Object> param) throws BusinessException{
    roleManageService.relateMenu(param);
    return Result.success();
  }
}
